import Vue from "vue";
import VueRouter from "vue-router";
import vuex from "@/store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { Message } from "element-ui";
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

NProgress.configure({ showSpinner: false }); // 配置NProgress
const IndexView = () => import("@/views/IndexView.vue");
const UserCenter = () => import("@/views/user/UserCenter.vue");
const UserSetting = () => import("@/views/user/children/UserSetting.vue");
const Welcome = () => import("@/views/user/children/UserCenterWelcome.vue");
const SendBlog = () => import("@/components/layout/SendBlogPost.vue");
const ShowUserBlog = () => import("@/views/user/children/ShowUserBlog.vue");
const ShowBlog = () => import("@/views/blog/ShowBlog.vue");
const ShowClassify = () => import("@/views/rightCard/ShowClassify.vue");
const ShowTag = () => import("@/views/rightCard/ShowTag.vue");
const ShowLogin = () => import("@/views/login/ShowLogin.vue");
const ShowFavoriteBlog = () => import("@/views/user/children/FavoriteBlog.vue");
const ShowMessage = () => import("@/components/layout/ShowMessage.vue");
const ArchivesPage = () => import("@/components/layout/ArchivesPage.vue");
const MyBlogData = () => import("@/views/user/children/MyBlogData.vue");
Vue.use(VueRouter);
/**
 * 自定义route信息配置, 其中配置所有route信息,path name
 */
const constantRoutes = [
  {
    path: "",
    // 重定向路由
    redirect: "/index",
    // 是否需要拦截
    meta: {
      auth: false,
    },
  },
  {
    path: "/index",
    component: IndexView,
    meta: {
      auth: false,
    },
  },
  {
    path: "/showTag",
    component: ShowTag,
    meta: {
      auth: false,
    },
  },
  {
    path: "/showBlog/:id",
    name: "blog",
    component: ShowBlog,
    meta: {
      auth: false,
    },
  },
  {
    path: "/message",
    name: "message",
    component: ShowMessage,
    meta: {
      auth: false,
    },
  },
  {
    path: "/showLogin",
    component: ShowLogin,
    meta: {
      auth: false,
    },
  },
  {
    path: "/showClassify",
    component: ShowClassify,
    meta: {
      auth: false,
    },
  },
  {
    path: "/archivesPage",
    component: ArchivesPage,
    meta: {
      auth: false,
    },
  },
  {
    path: "/userCenter",
    component: UserCenter,
    redirect: "/welcome",
    meta: {
      auth: true,
    },
    children: [
      {
        path: "/userSetting", //使用占位符声明接收params参数
        component: UserSetting,
        meta: {
          auth: true,
        },
      },
      {
        path: "/myData",
        component: MyBlogData,
        meta: {
          auth: true,
        },
      },
      {
        path: "/welcome",
        component: Welcome,
        meta: {
          auth: true,
        },
      },
      {
        path: "/sendBlog",
        name: "sendBlog",
        component: SendBlog,
        meta: {
          auth: true,
        },
      },
      {
        path: "/showUserBlog",
        component: ShowUserBlog,
        meta: {
          auth: true,
        },
      },
      {
        path: "/showFavoriteBlog",
        component: ShowFavoriteBlog,
        meta: {
          auth: true,
        },
      },
    ],
  },
];

// 创建自定义router

export const router = new VueRouter({
  mode: "hash",
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    return {
      x: 0,
      y: 0,
    };
  },
});
router.beforeEach((to, from, next) => {
  NProgress.start();
  // chrome
  document.body.scrollTop = 0;
  // firefox
  document.documentElement.scrollTop = 0;
  // safari
  window.pageYOffset = 0;
  let localToken = localStorage.getItem("auth");
  let tokenTime = localStorage.getItem("tokenTime");

  const timeOver = 7 * 24 * 3600 * 1000;
  // 当前时间
  let date = new Date().getTime();
  // token 验证是否过期
  if (localToken && date - tokenTime > timeOver) {
    // 清空数据
    localStorage.removeItem("auth");
    localStorage.removeItem("tokenTime");
    localStorage.removeItem("store");
    Message.error("登录信息已过期，请重新登录");
  }
  if (to.meta.auth) {
    if (localToken && localToken.length > 0) {
      next();
    } else {
      Message.error("你还没有登录哦");
      NProgress.done();
      router.push("./");
    }
  } else {
    next();
  }
});
router.afterEach(() => {
  NProgress.done();
  window.scrollTo(0, 0);
});

export default router;

//暴露
